package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.task.ComputeUtil;
import com.tanhua.model.domain.Analysis;
import com.tanhua.model.vo.AnalysisVo;
import com.tanhua.model.vo.DashboardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DashboardService {
    @Autowired
    private AnalysisMapper analysisMapper;

    public AnalysisVo summary() {
        //今天
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        //昨天
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();
        //前七天
        String weekday = DateUtil.offsetDay(new Date(), -7).toDateStr();
        //前30天
        String monthday = DateUtil.offsetDay(new Date(), -30).toDateStr();
        Integer cumulativeUsers = null;
        long activePassMonth = 0;
        long activePassWeek = 0;
        Integer newUsersToday = null;
        BigDecimal newUsersTodayRate = null;
        Integer loginTimesToday = null;
        BigDecimal loginTimesTodayRate = null;
        Integer activeUsersToday = null;
        BigDecimal activeUsersTodayRate = null;

        try {
            //今日内容
            Analysis todayAna = analysisMapper.findBase(today);
            //昨天内容
            Analysis yesterdayAna = analysisMapper.findBase(yesterday);
            //累计用户数
            cumulativeUsers = analysisMapper.findCumulativeUsers();
            //过去30天活跃用户数
            activePassMonth = analysisMapper.findActivePass(monthday, today);
            //过去7天活跃用户
            activePassWeek = analysisMapper.findActivePass(weekday, today);
            //  今日新增用户数量
            newUsersToday = todayAna.getNumRegistered();
            // 今日新增用户涨跌率，单位百分数，正数为涨，负数为跌   BigDecimal : 商业数字格式
            Integer yesterdayReg = yesterdayAna.getNumRegistered();
            newUsersTodayRate = ComputeUtil.computeRate(newUsersToday, yesterdayReg);
            //今日登录次数
            loginTimesToday = todayAna.getNumLogin();
            //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
            Integer yesterdayLogin = yesterdayAna.getNumLogin();
            loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, yesterdayLogin);
            //今日活跃用户数量
            activeUsersToday = todayAna.getNumActive();
            //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
            Integer yesterdayActive = yesterdayAna.getNumActive();
            activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, yesterdayActive);
        } catch (Exception e) {
            e.printStackTrace();
        }

        AnalysisVo vo = null;
        try {
            vo = new AnalysisVo();
            vo.setCumulativeUsers(Integer.valueOf(cumulativeUsers.toString()));
            vo.setActivePassMonth(Integer.valueOf((int) activePassMonth));
            vo.setActivePassWeek(Integer.valueOf((int) activePassWeek));
            try {
                vo.setLoginTimesTodayRate(loginTimesTodayRate.compareTo(activeUsersTodayRate));
                vo.setNewUsersTodayRate(newUsersTodayRate.compareTo(activeUsersTodayRate));
                vo.setActiveUsersTodayRate(activeUsersTodayRate.compareTo(loginTimesTodayRate));
            } catch (Exception e) {
                e.printStackTrace();
            }
            vo.setNewUsersToday(newUsersToday);
            vo.setLoginTimesToday(loginTimesToday);
            vo.setActiveUsersToday(activeUsersToday);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return vo;
    }

    public Map users(Long sd, Long ed, Integer type) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(sd);
        Date date = c.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //今年开始时间
        String startDate = simpleDateFormat.format(date);
        //去年开始时间
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String lastYearStartDate = simpleDateFormat.format(y);
        //今年结束时间
        c.setTimeInMillis(ed);
        Date time = c.getTime();
        String endDate = simpleDateFormat.format(time);
        //去年结束时间
        c.add(Calendar.YEAR, -1);
        Date x = c.getTime();
        String lastYearEndDate = simpleDateFormat.format(x);

        QueryWrapper<Analysis> query = new QueryWrapper<>();
        query.between("record_date", startDate, endDate);
        //今年所选时间数据
        List<Analysis> thisYear = analysisMapper.selectList(query);
        List<DashboardVo> thisYearVo = new ArrayList<>();
        DateUtil.year(date);

        if (!CollUtil.isEmpty(thisYear)) {
            for (Analysis analysis : thisYear) {
                DashboardVo vo = new DashboardVo();
                Date datex = analysis.getRecordDate();
                //获取日期中的天
                Integer dateDate = datex.getDate();
                //设置时间
                vo.setTitle(dateDate.toString());

                if (type == 101) {
                    vo.setAmount(analysis.getNumRegistered());
                } else if (type == 102) {
                    vo.setAmount(analysis.getNumActive());
                } else {
                    vo.setAmount(analysis.getNumRetention1d());
                }
                thisYearVo.add(vo);
            }
        }

        /*去年时间段的数据*/
        query = new QueryWrapper<>();
        query.between("record_date", lastYearStartDate, lastYearEndDate);
        List<Analysis> lastYear = analysisMapper.selectList(query);
        List<DashboardVo> lastYearVo = new ArrayList<>();
        if (!CollUtil.isEmpty(lastYear)) {
            for (Analysis analysis : lastYear) {
                DashboardVo Vo = new DashboardVo();
                Date datex = analysis.getRecordDate();
                //获取日期中的天
                Integer dateDate = datex.getDate();
                //设置时间
                Vo.setTitle(dateDate.toString());

                if (type == 101) {
                    Vo.setAmount(analysis.getNumRegistered());
                } else if (type == 102) {
                    Vo.setAmount(analysis.getNumActive());
                } else {
                    Vo.setAmount(analysis.getNumRetention1d());
                }
                lastYearVo.add(Vo);
            }
        }
        Map map = new HashMap();
        map.put("thisYear", thisYearVo);
        map.put("lastYear", lastYearVo);
        return map;
    }


}